﻿using System;
using System.Linq;
using Landpy.CruiseControlNet.FacadeInterface.ConfigurationModule;
using Landpy.CruiseControlNet.FacadeInterface.RepositoryModule;
using Landpy.CruiseControlNet.Infrastructure.Aop.WrapAttribute;

namespace Landpy.CruiseControlNet.DomainLayer.RepositoryModule
{
    public class EmailGroupRepository : IEmailGroupRepository
    {
        private IProjectRepository ProjectRepository { get; set; }
        private IPublisherRepository PublisherRepository { get; set; }

        public EmailGroupRepository()
        {
            this.ProjectRepository = new ProjectRepository();
            this.PublisherRepository = new PublisherRepository();
        }

        [NotThrowException]
        [ExceptionMessageBox]
        [LogException]
        public Group GetGroupById(Project project, Guid emailId, Guid id)
        {
            Group queryGroup = null;
            if (project != null)
            {
                var email = this.PublisherRepository.GetPublisherById(project, emailId) as Email;
                if (email != null)
                {
                    queryGroup = (from Group @group in email.Groups
                                  where @group.Id == id
                                  select @group).FirstOrDefault();
                }
            }
            return queryGroup;
        }

        [NotThrowException]
        [ExceptionMessageBox]
        [LogException]
        public Group GetGroupById(CruiseControl cruiseControl, Guid projectId, Guid emailId, Guid id)
        {
            var queryProject = this.ProjectRepository.GetProjectById(cruiseControl, projectId);
            return this.GetGroupById(queryProject, emailId, id);
        }
    }
}